package com.xml.dom.read;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;


public class CSVWriter {
	
	private static final String COMMA = ",";

	private List<BookDTO> books;
	
	public CSVWriter(List<BookDTO> books) {
		this.books = books;
	}
	
	public void write(String name) {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(name));
			for(BookDTO b : books) {
				bw.write(this.getCommaData(b));
				bw.write("\n");
			}
			bw.close();
		} catch (IOException e) {
			System.err.println("Can't write CSV File.");
		}
	}
	
	private String getCommaData(BookDTO book) {
		StringBuilder sb = new StringBuilder();
		sb.append(book.getName()).append(COMMA);
		sb.append(book.getPrice()).append(COMMA);
		sb.append(book.getAuthor());
		return sb.toString();
	}
}
